package com.example.everying.config;

import com.wechat.pay.java.core.RSAPublicKeyConfig;
import com.wechat.pay.java.service.transferbatch.TransferBatchService;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.service.payments.h5.H5Service;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.core.util.PemUtil;

import lombok.Getter;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.io.IOException;

/**
 * @desc: 微信config
 * @author: szy
 * @date: 2025/5/15 
 */
@Configuration
@Getter
public class WeChatConfig {

    /**
     * 商户号
     */
    @Value("${wechat.pay.merchantId}")
    public String merchantId;
    /**
     * 商户API私钥路径
     */
    @Value("${wechat.pay.privateKeyPath}")
    public String privateKeyPath;

    @Value("${wechat.pay.publicKeyPath}")
    public String publicKeyPath;
    /**
     * 商户证书序列号
     */
    @Value("${wechat.pay.merchantSerialNumber}")
    public String merchantSerialNumber;

//    @Value("${wechat.pay.publicKeyId}")
    public String publicKeyId = "PUB_KEY_ID_0117169728372025051400321943001482";
    /**
     * 商户APIV3密钥
     */
//    @Value("${wechat.pay.apiV3Key}")
    public String apiV3Key = "5Tg8Rk9XmN2pL7yVq1sW3zB6dF4hJ0cA";
    /**
     * AppId
     */
    @Value("${wechat.pay.appId}")
    public String appId;

    @Value("${wechat.pay.payNotifyUrl}")
    public String payNotifyUrl;

    @Value("${wechat.pay.transferNotifyUrl}")
    public String transferNotifyUrl;

    private Config config;
    @Autowired
    private ResourceLoader resourceLoader;

    @PostConstruct
    public void initConfig() throws IOException {
        // 使用自动更新平台证书的RSA配置
        // 一个商户号只能初始化一个配置，否则会因为重复的下载任务报错
        config = new RSAPublicKeyConfig.Builder()
                .merchantId(merchantId)
                .privateKeyFromPath(privateKeyPath)
                .publicKeyFromPath(publicKeyPath)
                .publicKeyId(publicKeyId)
                .merchantSerialNumber(merchantSerialNumber)
                .apiV3Key(apiV3Key)
                .build();
    }

    @Primary
    @Bean()
    public H5Service h5Service() {
        return new H5Service.Builder()
                .config(config)
                .build();
    }

    @Primary
    @Bean()
    public JsapiService jsapiService() {
        return new JsapiService.Builder()
                .config(config)
                .build();
    }

    @Primary
    @Bean()
    public NativePayService nativePayService() {
        return new NativePayService.Builder()
                .config(config)
                .build();
    }


    @Primary
    @Bean
    public TransferBatchService transferBatchService() {
        return new TransferBatchService.Builder()
                .config(config)
                .build();
    }

    @Primary
    @Bean
    public NotificationParser notificationParser() {
        return new NotificationParser((NotificationConfig) config);
    }
}
